// pages/MusicIndex/musicDetails/musicDetails.js
const MusicHttp = require("../../../utils/MusicHttp")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        detialArr: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        var { id } = options;
        var mDetRes = await MusicHttp.getMusicListDetailById(id);
        this.handleMusicDet(mDetRes);
    },
    handleMusicDet(res) {
        res = res.data.playlist;
        res.playCount = this.handleCount(res.playCount);
        res.tracks.forEach(item => {
            item.mark = this.handleTime(item.mark);
            item.isPlay = false
        })
        this.setData({
            detialArr: res
        })
    },

    clickMe(e) {
        var { item } = e.currentTarget.dataset;
        let data = JSON.stringify(item);
        wx.navigateTo({
            url: `/pages/MusicIndex/playMusicDetail/playMusicDetail?item=${encodeURIComponent(data)}`,
        })
    },

    handleToggle(e) {
        var { aid } = e.currentTarget.dataset;
        var detialArr = this.data.detialArr;
        detialArr.tracks.forEach(item => {
            item.isPlay = item.id == aid ? !item.isPlay : "";
        })
        this.setData({
            detialArr
        })
    },

    handleShow(value) {
        if (value < 10) {
            return "0" + value
        } else {
            return "" + value
        }
    },
    handleTime(value) {
        var seconds = Math.floor(value / 1000)
        var minutes = this.handleShow(Math.floor(seconds / 60))
        seconds = this.handleShow(seconds % 60)
        var sum = minutes + ":" + seconds
        return sum
    },
    handleCount(num) {
        if (num > 100000000) {
            return Math.floor(num / 100000000) + '亿'
        } else if (num > 10000) {
            return Math.floor(num / 10000) + '万'
        } else {
            return num
        }
    },
})